// 文章的路由模块

const express = require('express')
const router = express.Router()

// 导入需要的处理函数模块
const article_handler = require('../router_handler/article')

// 导入 multer 和 path
const multer = require('multer')
const path = require('path')

// 创建 multer 的实例
let myFileName = ''
const uploads = multer({ 
  dest: path.join(__dirname, '../uploads'),
  filename: (req,file,cb) => {
        myFileName = `${file.fieldname}.${file.originalname.split('.').splice(-1)}`
        cb(null, myFileName)
      }
})
// const storage = multer({
//   destination: path.join(__dirname, '../uploads'),
//   filename: (req,file,cb) => {
//     myFileName = `${file.fieldname}.${file.originalname.split('.').splice(-1)}`
//     cb(null, myFileName)
//   }
// })
// let uploads = multer({storage})
// 导入验证数据的中间件
const expressJoi = require('@escook/express-joi')
// 导入需要的验证规则对象
const { add_article_schema } = require('../schema/article')

// 发布文章的路由
router.post('/add', uploads.single('img'), expressJoi(add_article_schema), article_handler.addArticle)
//router.post('/add', article_handler.addArticle)
module.exports = router
